<template>
  <div>
    <el-button type="danger" @click="openDialog">添加</el-button>
    <!-- 全部打开：default-expand-all -->
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="catename" label="分类名称"></el-table-column>
      <el-table-column prop="img" label="分类图片">
        <template slot-scope="scope">
          <img v-if="scope.row.img" width="150px" :src="'http://localhost:3000'+scope.row.img" />
          <!-- <img v-else width="150px" :src="img" /> -->
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status==1" type="success" size="medium">已启用</el-tag>
          <el-tag v-else type="danger" size="medium">已禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            size="mini"
            @click="edit(scope.row.id)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            size="mini"
            @click="del(scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="popType+'商品分类'" :visible.sync="dialogVisible" width="45%">
      <cate-info :transData="transData" :popType="popType"></cate-info>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="dialogVisible = false">取 消</el-button>
        <el-button type="danger" size="medium" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import cateInfo from "./info";
import { getCateList ,getCateById} from "../../request";
export default {
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      popType:'新增',
      transData:[],
    };
  },
  components: {
    cateInfo
  },
  created() {
    // 获取分类列表
    this.getData();
  },
  methods: {
    async getData() {
      let res = await getCateList();
      this.tableData = res;      
    },
    openDialog() {
      this.popType='新增'
      this.transData={
        pid: 0,
        catename: "",
        img: "",
        status: 1
      };
      this.dialogVisible = true;
    },
    async edit(id) {
      this.popType='修改';
      let res=await getCateById(id);
      this.transData=res;
      this.transData.id=id;
      this.dialogVisible=true;
    },
    del(id) {
      this.$confirm("确认删除吗,是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type:"warning",
        confirmButtonClass:"el-button--danger"
      })
        .then(res => {
          this.doDel(id);
        })
        .catch(_ => {});
    },
    doDel(id) {
      this.$http.post('/catedelete', {id}).then(res => {
        this.tableData = res.data.list;
      });
    }
  }
};
</script>
<style lang="" >
.el-button {
  margin-bottom: 10px;
}
.el-pagination__total {
  float: right;
}
</style>